﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.IO;
using Bitis_KeToanThue_Pro_DungChung.Common;
using Bitis_KeToanThue_Pro_QuanTri.AccessDatabase;
namespace Bitis_KeToanThue_Pro_QuanTri.Form
{
    public partial class frmDanhMucMenu : Telerik.WinControls.UI.RadForm
    {

        DanhMucMenu dm = new DanhMucMenu();
        public static string option = "";
        public frmDanhMucMenu()
        {
            InitializeComponent();
            khoitaogrid();
            ClassXuLyChung.loadcombodungchung(cbLoaiMenu, dm.loadcomboloaimenu(), "DienGiai", "MaLoaiMenu");
            loadgrid();
            trangthaisudungcontrol(false);
            trangthaisudung2button(true);
            trangthaisudung3button(false);
            UpdatePanelInfo(this.radGridView1.CurrentRow);
            if (BienHeThong.ht_quyencapnhat == 1)
            {
                btnSua.Enabled = true;
                btnThem.Enabled = true;
            }
            else
            {
                btnSua.Enabled = false;
                btnThem.Enabled = false;
            }
            if (BienHeThong.ht_quyenxoa == 1)
            {
                btnXoa.Enabled = true;
            }
            else
            {
                btnXoa.Enabled = false;
            }
        }
        private void UpdatePanelInfo(GridViewRowInfo currentRow)
        {
            if (currentRow != null && !(currentRow is GridViewNewRowInfo))
            {
            
                txtMenuID.Text = ClassXuLyChung.GetSafeString(currentRow.Cells[0].Value);
                txtTenMenu.Text = ClassXuLyChung.GetSafeString(currentRow.Cells[1].Value);
               txtDienGiai.Text = ClassXuLyChung.GetSafeString(currentRow.Cells[2].Value);
               cbLoaiMenu.SelectedValue = ClassXuLyChung.GetSafeInt(currentRow.Cells[8].Value);
               txtModule.Text= ClassXuLyChung.GetSafeString(currentRow.Cells[3].Value);
                 txtThuMuc.Text = ClassXuLyChung.GetSafeString(currentRow.Cells[4].Value);
                 txtTenForm.Text  = ClassXuLyChung.GetSafeString(currentRow.Cells[5].Value);
               DataTable dt = dm.loaddulieubyid(ClassXuLyChung.GetSafeInt(txtMenuID.Text));

               object photoValue = dt.Rows[0]["Hinh"];
                if (!Convert.IsDBNull(photoValue) && photoValue != null)
                {
                    pictureBoxIcon.Image = ClassXuLyChung.ConvertByeToImage((byte[])photoValue);//set image property of the picture box by creating a image from stream 
                    pictureBoxIcon.SizeMode = PictureBoxSizeMode.StretchImage;//set size mode property of the picture box to stretch 
                    pictureBoxIcon.Refresh();//refresh picture box
                }
                else
                {
                    pictureBoxIcon.Image = null;
                }

            }
            else
            {
                SoanLai();
                txtMenuID.Text = "";
            }
        }
        private void SoanLai()
        {
            txtTenMenu.Text = "";
            txtModule.Text = "";
            txtDienGiai.Text = "";
            txtThuMuc.Text = "";
           // cbLoaiMenu.Text = "";
            txtTenForm.Text = "";
            
            txtDuongDan.Text = "";
            pictureBoxIcon.Image = null;
            txtTenMenu.Focus();
        }
        private void trangthaisudung2button(bool trangthaidung)
        {
            btnThem.Enabled = trangthaidung;
            btnSua.Enabled = trangthaidung;
        }
        private void trangthaisudung1button(bool trangthaidung)
        {
            btnXoa.Enabled = trangthaidung;

        }
        private void trangthaisudung3button(bool trangthaidung)
        {
            btnLuu.Enabled = trangthaidung;
            btnKhong.Enabled = trangthaidung;
            btnNhapLai.Enabled = trangthaidung;
        }
        private void trangthaisudungcontrol(bool trangthaidung)
        {

            txtTenMenu.Enabled = trangthaidung;
            txtModule.Enabled = trangthaidung;
           txtDienGiai.Enabled = trangthaidung;
            txtThuMuc.Enabled = trangthaidung;
            cbLoaiMenu.Enabled = trangthaidung;
            txtTenForm.Enabled = trangthaidung;
            btnChonHinh.Enabled= trangthaidung;

            txtTenMenu.Focus();
        }
        private void loadgrid()
        {
            this.radGridView1.DataSource = dm.loadgrid();
        }
        private void khoitaogrid()
        {
            this.radGridView1.EnableHotTracking = true;
            this.radGridView1.ShowFilteringRow = false;
            this.radGridView1.EnableFiltering = true;
            this.radGridView1.MasterTemplate.ShowHeaderCellButtons = true;
            this.radGridView1.MasterTemplate.ShowFilteringRow = false;
            this.radGridView1.MasterTemplate.AutoExpandGroups = true;
            this.radGridView1.TableElement.RowHeight =32;
            this.radGridView1.MasterTemplate.AllowAddNewRow = false;
            this.radGridView1.MasterTemplate.AutoGenerateColumns = false;
            this.radGridView1.MasterTemplate.AllowEditRow = true;
            this.radGridView1.MasterTemplate.AllowDeleteRow = false;
            this.radGridView1.MasterTemplate.EnableSorting = true;
            this.radGridView1.MasterTemplate.AllowAutoSizeColumns = true;
            this.radGridView1.AutoScroll = true;
            this.radGridView1.EnableAlternatingRowColor = true;

            GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn()
            {
                FieldName = "MenuID",
                Name = "MenuID",
                HeaderText = "ID",
                Width = 50,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn);

            GridViewTextBoxColumn textBoxColumn1 = new GridViewTextBoxColumn()
            {
                FieldName = "TenMenu",
                Name = "TenMenu",
                HeaderText = "Tên Menu",
                Width = 248,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn1);

            GridViewTextBoxColumn textBoxColumn2 = new GridViewTextBoxColumn()
            {
                FieldName = "DienGiai",
                Name = "DienGiai",
                HeaderText = "Diễn Giải",
                Width = 240,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn2);

            GridViewTextBoxColumn textBoxColumn3 = new GridViewTextBoxColumn()
            {
                FieldName = "TenModule",
                Name = "TenModule",
                HeaderText = "Module",
                Width = 200,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn3);

            GridViewTextBoxColumn textBoxColumn4 = new GridViewTextBoxColumn()
            {
                FieldName = "TenThuMuc",
                Name = "TenThuMuc",
                HeaderText = "Thư Mục",
                Width = 110,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn4);

            GridViewTextBoxColumn textBoxColumn5 = new GridViewTextBoxColumn()
            {
                FieldName = "TenForm",
                Name = "TenForm",
                HeaderText = "Tên Form",
                Width = 200,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn5);

           

            GridViewImageColumn imagecolumn = new GridViewImageColumn()
            {
                FieldName = "Hinh",
                Name = "Hinh",
                HeaderText = "Icon",
                Width = 32,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter,
                ImageLayout= ImageLayout.Stretch,
                ImageAlignment=ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(imagecolumn);


            GridViewTextBoxColumn textBoxColumn6 = new GridViewTextBoxColumn()
            {
                FieldName = "TenLoai",
                Name = "TenLoai",
                HeaderText = "Loại Menu",
                Width = 150,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn6);

            GridViewTextBoxColumn textBoxColumn7 = new GridViewTextBoxColumn()
            {
                FieldName = "MaLoaiMenu",
                Name = "MaLoaiMenu",
                HeaderText = "MaLoaiMenu",
                Width = 107,
                IsVisible = false,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn7);




        }
        public void openfile()
        {
            OpenFileDialog OPF = new OpenFileDialog()
            {
                Title = "Open File Image", 
                Filter = "Image File (.JPG,.PNG,.ICO)|*.jpg;*.png;*.ico"
            };
            if (OPF.ShowDialog() == DialogResult.OK)
            {
                txtDuongDan.Text = Path.GetFullPath(OPF.FileName).Trim();

            }
        }
        private void btnChonHinh_Click(object sender, EventArgs e)
        {
            openfile();
            if (txtDuongDan.Text != "")
            {
                pictureBoxIcon.Image = Image.FromFile(txtDuongDan.Text);
                pictureBoxIcon.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBoxIcon.Refresh();
            }
            else
            {
                pictureBoxIcon.Image = null;
            }
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            ClassXuLyChung.tuychontimkiemtrenluoi(txtTimKiem, radGridView1, 0, 0);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            option = "Them";
            SoanLai();
            trangthaisudungcontrol(true);
            trangthaisudungluoi(false);
            trangthaisudung2button(false);
            trangthaisudung1button(false);
            trangthaisudung3button(true);
        }
        private void trangthaisudungluoi(bool trangthaidung)
        {
            radGridView1.Enabled = trangthaidung;
        }
        private void btnSua_Click(object sender, EventArgs e)
        {
            option = "Sua";
            trangthaisudungcontrol(true);
            trangthaisudungluoi(true);
            trangthaisudung2button(false);
            trangthaisudung1button(false);
            trangthaisudung3button(true);
        }

        private void btnKhong_Click(object sender, EventArgs e)
        {
            option = "";
            trangthaisudungcontrol(false);
            trangthaisudungluoi(true);
            trangthaisudung2button(true);
            trangthaisudung1button(true);
            trangthaisudung3button(false);
            loadgrid();
            UpdatePanelInfo(this.radGridView1.CurrentRow);
        }

        private void btnNhapLai_Click(object sender, EventArgs e)
        {
            SoanLai();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtMenuID.Text))
            {
                DialogResult kq = RadMessageBox.Show(String.Format("Bạn có muốn chắc xóa menu \n : {0} không ???",txtTenMenu.Text), "Thông Báo", MessageBoxButtons.YesNo, RadMessageIcon.Question);
                if (kq == DialogResult.Yes)
                {

                    if (dm.xoa(int.Parse(txtMenuID.Text)) == 1)
                    {
                        RadMessageBox.Show("Xóa Thành Công menu \n : " + txtTenMenu.Text, "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                        SoanLai();
                        loadgrid();
                        UpdatePanelInfo(this.radGridView1.CurrentRow);
                    }
                    else
                    {
                        RadMessageBox.Show("Menu này không tồn tại ", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                    }
                }

            }
            else
            {
                RadMessageBox.Show("Không tồn tại dữ liệu ", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
            }
        }

        private void radGridView1_CellClick(object sender, GridViewCellEventArgs e)
        {
            UpdatePanelInfo(this.radGridView1.CurrentRow); 
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtTenMenu.Text != "" && txtModule.Text != "" && txtTenForm.Text !="")
            {
                if (option == "Them")
                {
                    if (dm.them(txtTenMenu.Text,txtModule.Text,txtTenForm.Text,txtDienGiai.Text,txtDuongDan.Text,ClassXuLyChung.GetSafeInt(cbLoaiMenu.SelectedValue),txtThuMuc.Text) == 1)
                    {
                        RadMessageBox.Show("Thêm Thành Công Menu : " + txtTenMenu.Text, "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                    }
                    else
                    {
                        RadMessageBox.Show(String.Format("Menu : {0} đã tồn tại", txtTenMenu.Text), "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                    }
                }
                else
                {
                    if (option == "Sua")
                    {
                        if (txtMenuID.Text != "")
                        {

                            if (dm.sua(txtTenMenu.Text, txtModule.Text, txtTenForm.Text, txtDienGiai.Text, txtDuongDan.Text, ClassXuLyChung.GetSafeInt(cbLoaiMenu.SelectedValue), txtThuMuc.Text,ClassXuLyChung.GetSafeInt(txtMenuID.Text)) == 1)
                            {
                                RadMessageBox.Show("Cập Nhật Thành Công Menu : " + txtTenMenu.Text, "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                            }
                            else
                            {
                                RadMessageBox.Show("Menu này đã tồn tại", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                            }
                        }
                        else
                        {
                            RadMessageBox.Show("Không tồn tại dữ liệu ", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                        }
                    }
                }
                option = "";
                trangthaisudungcontrol(false);
                trangthaisudungluoi(true);
                trangthaisudung2button(true);
                trangthaisudung1button(true);
                trangthaisudung3button(false);
                SoanLai();
                loadgrid();
                UpdatePanelInfo(this.radGridView1.CurrentRow);

            }
            else
            {
                RadMessageBox.Show("Chưa Nhập Đầy Đủ Thông Tin. ", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
            }
        }

      
     
    }
}
